﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace StudentManagement
{
    public partial class frmCourseM : Form
    {
        public frmCourseM()
        {
            InitializeComponent();
        }

        private void tianjia_Click(object sender, EventArgs e)
        {
            frmKecheng f = new frmKecheng();
            DialogResult dr = f.ShowDialog();
            if (dr==DialogResult.OK)
            {
                BindingData();   
            }
            
            
        }
        public void BindingData()
        {
            string connstr = "server=.;database=Class;user=sa;password=123456";
            SqlConnection conn = new SqlConnection(connstr);
            conn.Open();
            string selectsql = "SELECT Id,Name FROM Subject";
            SqlCommand comm = new SqlCommand(selectsql, conn);
            SqlDataReader dr = comm.ExecuteReader();
            List<StudentInfo> stuList = new List<StudentInfo>();
            while (dr.Read())
            {
                StudentInfo s = new StudentInfo();
                s.Id = dr.GetString(0);
                s.Name = dr.GetString(1);
                stuList.Add(s);
            }
            conn.Close();
            this.kecheng.DataSource = stuList;
        }

        private void frmCourseM_Load(object sender, EventArgs e)
        {
            BindingData();
        }

        private void shanchu_Click(object sender, EventArgs e)
        {
            string id = this.kecheng.SelectedRows[0].Cells[0].Value.ToString();
            string connstr = "server=.;database=Class;user=sa;password=123456";
            SqlConnection conn = new SqlConnection(connstr);
            conn.Open();
            string sql = "delete from subject where id='" + id + "'";
            SqlCommand comm = new SqlCommand( sql,conn);
            int count =  comm.ExecuteNonQuery();
            if (count > 0)
            {
                BindingData();
            }
            
        }

        private void kecheng_DoubleClick(object sender, EventArgs e)
        {
           
            DataGridViewRow dgvr = this.kecheng.SelectedRows[0];
            StudentInfo s = new StudentInfo();
           s.Id = dgvr.Cells[0].Value.ToString();
            s.Name = dgvr.Cells[1].Value.ToString();
            frmKecheng f = new frmKecheng();
            f.StudentInfo = s;
            f.Text = "编辑课程信息";
            DialogResult dr = f.ShowDialog();
            if (dr==DialogResult.OK)
            {
                BindingData();
            }

        }



        
    
            
    }
}
